package main

import (
	"fmt"
	"time"
)

// Go supports anonymous functions, which can form closures. Anonymous functions are useful when you want to define a function inline without having to name it.
// Go 支持匿名函数，并能用其构造闭包。 匿名函数在你想定义一个不需要命名的内联函数时是很实用的。
func intSeq() func() int {
	i := 0

	return func() int {
		i++
		return i
	}
}

func Closures() {
	strArr := []string{"a", "b", "c"}
	for _, v := range strArr {
		// v1 := v[:]
		fmt.Println(v, &v)
		go func() {
			fmt.Println(v, &v)
		}()
	}

	time.Sleep(1 * time.Second)

	str := "abc"
	str1 := str[:]
	fmt.Println(&str, &str1)
	fmt.Println(str1)

	nextInt := intSeq()
	aInt := intSeq()

	fmt.Println(nextInt())
	fmt.Println(nextInt())
	fmt.Println(nextInt())
	fmt.Println(aInt())

	// newInts := intSeq()
	// fmt.Println(newInts())

	// 1
	// 2
	// 3
	// 1
}

func aaaaaaa() int {
	var i = 0
	i++
	return i
}

func main() {
	Closures()
}
